package com.stedu.mall.goodsservice.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.Category;
import com.stedu.mall.common.bean.Goods;
import com.stedu.mall.common.exception.MallException;
import com.stedu.mall.common.service.GoodsService;
import com.stedu.mall.goodsservice.mapper.CategoryMapper;
import com.stedu.mall.goodsservice.mapper.GoodsMapper;
import com.stedu.mall.goodsservice.mapper.GoodsPicMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@DubboService
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsPicMapper goodsPicMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
        *1. 所属分类是否存在，并且必须是子分类
        * 2. 不能添加同名商品
            * 分类
            * 名字
            * 颜色
            * 版本
        3. 添加商品
        4. 添加商品图片
        * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(Goods goods) throws MallException {
        //所属分类是否存在，必须是子分类
        Category category = categoryMapper.selectById(goods.getCategoryId());
        if (category == null || (category != null && category.getParentId().equals(0))){
            throw new MallException("所属分类不存在或者分类是父分类无法添加");
        }
        /*
         * 2. 不能添加同名商品
         *    分类
         *    名字
         *    颜色
         *    版本
         * */
        Goods condition = new Goods();
        condition.setCategoryId(goods.getCategoryId());
        condition.setName(goods.getName());
        condition.setColor(goods.getColor());
        condition.setVersion(goods.getVersion());

        if (goodsMapper.selectByCondition(condition)
                .stream()
                .anyMatch(item -> item.getName().equals(goods.getName()))) {
            throw new MallException("存在相同商品，无法添加");
        }

        //添加商品
        goodsMapper.insert(goods);
        //判断是否图片需要添加
        if (goods.getPicList()!= null && goods.getPicList().size() > 0) {
            //添加图片的商品id
            goods.getPicList()
                    .stream()
                    .forEach(pic -> pic.setGoodsId(goods.getId()));
            goodsPicMapper.insert(goods.getPicList());
        }
    }
    /**
     * 判断商品是否存在
     * 判断是否关联订单
     * 删除商品图片
     * 删除商品
     * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws MallException {
        //判断商品是否存在
        if (goodsMapper.selectById(id) == null) {
            throw new MallException("商品不存在，无法删除");
        }
        //todo 判断是否关联订单
        //删除商品图片
        goodsPicMapper.delete(id);
        //删除商品
        goodsMapper.delete(id);

    }

    /**
     * 判断商品是否存在
     * 判断修改之后的分类是否存在 —— 前提是必须修改分类
     * 不能同名
     * 修改商品信息
     * 修改图片
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Goods goods) throws MallException {
        Goods newInfo = goods;
        Goods oldInfo = goodsMapper.selectById(goods.getId());
        //判断商品是否存在
        if (oldInfo == null) {
            throw new MallException("商品不存在，无法修改");
        }
        //判断是否修改了分类
        if (newInfo.getCategoryId() != null && !newInfo.getCategoryId().equals(oldInfo.getCategoryId())){
            //判断修改之后的分类是否存在并且必须是子分类
            Category category = categoryMapper.selectById(newInfo.getCategoryId());
            if (category == null ||(category != null && category.getParentId().equals(0))) {
                throw new MallException("修改之后的分类不存在或者修改之后的分类是父分类无法修改");
            }
        }
        //不能同名
        /*
         * 2. 不能添加同名商品
         *    分类
         *    名字
         *    颜色
         *    版本
         * */
        Integer newCategoryId = newInfo.getCategoryId() == null ? oldInfo.getCategoryId() : newInfo.getCategoryId();
        String newName = newInfo.getName() == null ? oldInfo.getName() : newInfo.getName();
        String newColor = newInfo.getColor() == null? oldInfo.getColor() : newInfo.getColor();
        String newVersion = newInfo.getVersion() == null? oldInfo.getVersion() : newInfo.getVersion();
        Goods condition = new Goods();
        condition.setCategoryId(newCategoryId);
        condition.setName(newName);
        condition.setColor(newColor);
        condition.setVersion(newVersion);

        if (goodsMapper.selectByCondition(condition)
                .stream()
                .anyMatch(item -> item.getName().equals(newName) && !item.getId().equals(newInfo.getId()))) {
            throw new MallException("存在相同商品，无法修改");
        }

        //修改商品信息
        goodsMapper.update(goods);

        /*
         * 修改图片
         * 1. 删除之前的图片
         * 2. 添加
         * */
        if (newInfo.getPicList() != null && newInfo.getPicList().size() > 0){
            goodsPicMapper.delete(newInfo.getId());
            newInfo.getPicList()
                    .stream()
                    .forEach(item -> item.setGoodsId(newInfo.getId()));
            //添加图片
            goodsPicMapper.insert(newInfo.getPicList());
        }
    }

    @Override
    public Goods selectById(Integer id) {
        return goodsMapper.selectById(id);
    }


    @Override
    public PageInfo<Goods> selectByCondition(Goods condition, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Goods> goods = goodsMapper.selectByCondition(condition);
        PageInfo<Goods> pageInfo = new PageInfo<>(goods);
        return pageInfo;
    }

    @Override
    public PageInfo<Goods> selectByCondition1(Goods condition, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询
        List<Goods> goods = goodsMapper.selectByCondition1(condition);
        PageInfo<Goods> pageInfo = new PageInfo<>(goods);
        return pageInfo;
    }

    @Override
    public List<Map<String, Object>> goodsSaleTop5() {

        List<Map<String, Object>> list = goodsMapper.goodsSaleTop5();
        return list;
    }
}
